package com.youbug.mall.product_pms.feign;

import com.youbug.common.vo.R;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.AsyncContext;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class PmsToSmsThreadPool {
    private static ExecutorService pool = Executors.newFixedThreadPool(5);

    public static R run(Runnable runnable, AsyncContext context){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();

        RequestContextHolder.setRequestAttributes(servletRequestAttributes,true);
        Future<R> future = pool.submit(runnable, R.ok());
        try {
            R r = future.get();
            return r;
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }finally {
            context.complete();
        }
        return null;
    }
}
